package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author hyh
 * @version 1.0
 */
@Slf4j
@Service
@SuppressWarnings({"all"})
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFavorMapper dishFavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 添加菜品和口味
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
//        for (DishFlavor f : flavors ){
//            f.setDishId(dishDTO.getId());
//            dishFavorMapper.insert(f);
//        }
        Long id = dish.getId();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
            dishFavorMapper.insert(flavors);
        }

    }

    /**
     * 菜品的分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageDish(DishPageQueryDTO dishPageQueryDTO) {
        //分页查询设置起始页码，和每页大小
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //数据库查询的结果是一个page的集合
        Page<DishVO> dishes =dishMapper.pageSelect(dishPageQueryDTO);
        List<DishVO> result = dishes.getResult();
        log.info("输出结果是{}", result);
        long total = dishes.getTotal();
        return new PageResult(total, result);
    }

    /**
     * 批量删除菜品
     * 同时管理三个表，dish ， dishflavor ，setmeal_dish
     * 删除菜品的同时需要同时删除菜的口味表中菜品的口味，以及判断套餐中是否有这个菜，再决定是否能删除菜品
     *
     * @param ids
     */
    @Transactional
    @Override
    public int delete(List<Long> ids) {
        int states = dishMapper.isSell(ids);
        log.info("查询到的菜品中处于起售中的有{}个", states);
        int nums = setmealDishMapper.countNum(ids);
        log.info("查询到套餐中{}条数据", nums);
        if (states != 0) {
            //代表删除的菜品中有正卖的
            return StatusConstant.ON_SALE;
        }
        if (nums != 0) {
            //代表套餐中有要删除的菜，不能删除
            return StatusConstant.HAVING;
        }
        //删除菜品和口味
        dishMapper.deleteDish(ids);
        dishFavorMapper.deleteFavor(ids);
        //0 代表可以删除删除完成
        return 0;
    }

    /**
     * 根据id查询用于数据回写
     *
     * @param id
     * @return
     */
    @Override
    public DishDTO selectById(Integer id) {
        DishDTO dishDTO = new DishDTO();
        Dish dish = dishMapper.selectById(id);
        List<DishFlavor> dishFlavors = dishFavorMapper.selectById(id);
        BeanUtils.copyProperties(dish, dishDTO);
        dishDTO.setFlavors(dishFlavors);
        return dishDTO;
    }

    /**
     * 用于修改菜品内容
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        dishMapper.update(dish);
        //给每个口味赋菜品id值
        if (flavors != null && flavors.size() != 0) {
            flavors.stream()
                    .forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
            List<Long> longs = new ArrayList<>();
            longs.add(dish.getId());
            dishFavorMapper.deleteFavor(longs);
            dishFavorMapper.insert(flavors);
        }

    }

    /**
     * 改变菜品的销售状态
     * @param status
     * @param id
     */

    @Override
    public void ifSale(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);

    }

    @Override
    public List<Dish> selectByCategoryId(Integer categoryId) {
        List<Dish> dishes = dishMapper.selectByCategoryId(categoryId);
        return dishes;
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFavorMapper.selectById(d.getId().intValue());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
